import 'package:dio/dio.dart';
import 'package:f_itshop/pages/home/home_api.dart';
import 'package:f_itshop/service/requests.dart';
import 'package:flutter/material.dart';

class HomePage extends StatefulWidget {
  const HomePage({super.key});

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  @override
  void initState() {
    //测试单例类
    // XTXRequestManager m1 = XTXRequestManager();
    // XTXRequestManager m2 = XTXRequestManager();
    //print(identical(m1.dio, m2.dio));

    // 获取首页网络数据
    _loadData();
    super.initState();
  }

  /// 发送网络请求获取首页的网络数据
  void _loadData() async {
    // 1.准备首页的请求地址 GET
    //String path = 'https://pcapi-xiaotuxian-front.itheima.net/home/index';
    // 2.创建 http client
    //Dio dio = Dio();
    // 3.获取请求结果
    try {
      //Response response = await dio.get(path);

      //使用网络请求单例类中的Dio实例发送获取首页网络数据
      //Response response = await XTXRequestManager().dio!.get(path);
      //Response response = await XTXRequestManager().dio!.get('home/index');
      // 使用处理请求的公共方法发送请求获取首页网络数据
      // Response response = await XTXRequestManager().handleRequest(
      //   'home/index',
      //   'GET',
      // );
      Response response = await HomeApi.homeFetch();
      print(response.data);
    } catch (e) {
      // 捕获异常
      print(e);
    }
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      alignment: Alignment.center,
      child: Text('首页', style: TextStyle(fontSize: 20.0)),
    );
  }
}
